﻿/*
 * This file is a part of the open source project http://meshsharp.codeplex.com
 * 
 * Author: Vladimir Vasiliev
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MeshSharp.Core.Models;
using System.Drawing;
using MeshSharp.Core;
using System.Xml.Serialization;

namespace PathFinder2d
{
    public class Data
    {
        List<Vector3> verts = new List<Vector3>();
        List<Vector3> allPoints = new List<Vector3>();
        Vector3 start = new Vector3(20, 70, 0);
        Vector3 end = new Vector3(30, 80, 0);
        List<int> indices = new List<int>();

        public Vector3 Start
        {
            get
            {
                return start;
            }
            set
            {
                start = value;
            }
        }

        public Vector3 End
        {
            get
            {
                return end;
            }
            set
            {
                end = value;
            }
        }

        [XmlIgnore]
        public List<Vector3> AllPoints
        {
            get
            {
                return allPoints;
            }
        }

        public void Reload()
        {
            allPoints.Clear();
            allPoints.Add(start);
            allPoints.Add(end);
            allPoints.AddRange(verts);
        }

        public List<Vector3> Vertexes
        {
            get
            {
                return verts;
            }
            set
            {
                verts = value;
            }
        }

        public List<int> Indices
        {
            get
            {
                return indices;
            }
            set
            {
                indices = value;
            }
        }

        public void AddPoints()
        {
            indices.Add(0);
            indices.Add(1);
            indices.Add(2);
            allPoints.Add(start);
            allPoints.Add(end);
            Add(new Vector3(10, 50, 0));
            Add(new Vector3(100, 50, 0));
            Add(new Vector3(10, 150, 0));
        }

        public void Add(Vector3 v)
        {
            verts.Add(v);
            allPoints.Add(v);
        }

        public List<Vector3> GetPath()
        {
            NavMesh polyMesh = new NavMesh(verts.ToArray(), indices.ToArray());
            return polyMesh.GetPath(start, end);
        }

        public void Draw(Graphics g, Font font)
        {
            for (int i = 0; i < verts.Count; ++i)
            {
                Vector3 v = verts[i];
                g.DrawEllipse(Pens.Green, v.X - 2, v.Y - 2, 4, 4);
                g.DrawString(i.ToString(), font, Brushes.Green, v.X, v.Y);
            }
            {
                Vector3 v = start;
                g.DrawEllipse(Pens.Red, v.X - 2, v.Y - 2, 4, 4);
                v = end;
                g.DrawEllipse(Pens.Red, v.X - 2, v.Y - 2, 4, 4);
            }
            int j = 0;
            int k = 1;
            while (j + 2 < indices.Count)
            {
                g.DrawLine(Pens.Blue, verts[indices[j]].X, verts[indices[j]].Y, verts[indices[j + 1]].X, verts[indices[j + 1]].Y);
                g.DrawLine(Pens.Blue, verts[indices[j + 2]].X, verts[indices[j + 2]].Y, verts[indices[j + 1]].X, verts[indices[j + 1]].Y);
                g.DrawLine(Pens.Blue, verts[indices[j + 2]].X, verts[indices[j + 2]].Y, verts[indices[j]].X, verts[indices[j]].Y);
                Vector3 v = verts[indices[j]] + verts[indices[j + 1]] + verts[indices[j + 2]];
                v /= 3;
                string str = k.ToString();
                SizeF strS = g.MeasureString(str, font);
                g.DrawString(str, font, Brushes.Blue, v.X - strS.Width / 2, v.Y - strS.Height / 2);
                ++k;
                j += 3;
            }
        }
    }
}
